package com.cloudcc.mobile.view.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.widget.CloudCCTitleBar;

import butterknife.Bind;

public class NewContactsActivity extends BaseActivity implements CloudCCTitleBar.OnTitleBarClickListener {
        @Bind(R.id.daoru_lian)
        Button daoru_lian;
        @Bind(R.id.headerbar)
        CloudCCTitleBar headerbar;
        @Bind(R.id.et_mobile)
        EditText et_mobile;
    private String username, usernumber;
    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initHeader();

        daoru_lian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Uri uri = ContactsContract.Contacts.CONTENT_URI;
                Intent intent = new Intent(Intent.ACTION_PICK, uri);
                startActivityForResult(intent,0);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                // ContentProvider展示数据类似一个单个数据库表
                // ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
                ContentResolver reContentResolverol = getContentResolver();
                // URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
                Uri contactData = data.getData();
                // 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
                Cursor cursor = managedQuery(contactData, null, null, null, null);
                cursor.moveToFirst();
                // 获得DATA表中的名字
                username = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // 条件为联系人ID
                String contactId = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
                // 获得DATA表中的电话号码，条件为联系人ID,因为手机号码可能会有多个
                Cursor phone = reContentResolverol.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                                + contactId, null, null);
                while (phone.moveToNext()) {
                    usernumber = phone
                            .getString(phone
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    et_mobile.setText(usernumber + " (" + username + ")");

                }

            }

        }



    @Override
    public int getLayoutId() {
        return R.layout.activity_new_contacts;
    }

    private void initHeader() {
        headerbar.setOnTitleBarClickListener(this);
    }

    @Override
    public void onClickLeft(View v) {

    }

    @Override
    public void onClickRight(View v) {

    }
}
